<template>
  <div class="app-container">
    <!--工具栏-->
    <!-- <Search :crud="crud">
      <el-form-item label="主题">
        <el-input v-model="query.theme" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="query.trainingType" placeholder="请选择">
          <el-option
            v-for="item in dict.TRAINING_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </Search> -->
    <el-card>
      <div class="head-container">
        <crudOperation :permission="permission">
          <template slot="right">
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="handle(null, 'new')"
              >新建</el-button
            >
            <!-- <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="handle(null,'batchDelete')">批量删除</el-button> -->
          </template>
        </crudOperation>
      </div>
      <el-table
        ref="table"
        border
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column prop="trainingType" label="内容" align="center">
          <template slot-scope="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="createdAtView"
          label="创建日期"
          width="200"
          align="center"
        /> -->
        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="handle(scope.row, 'delete')"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="handle(scope.row, 'update')"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </el-card>
    <TrainingDialog
      ref="contentDialog"
      @submitSuccess="submitSuccess"
    ></TrainingDialog>
  </div>
</template>

<script>
import crudJob from "@/api/system/job";
import CRUD, { presenter, header, crud } from "@crud/crud";
import crudOperation from "@crud/CRUD.operation";
import pagination from "@crud/Pagination";
import udOperation from "@crud/UD.operation";
import Search from "@/components/Search";
import pointsRule from "@/api/pointsRule/content";
import TrainingDialog from "./contentDialog.vue";
export default {
  name: "BrandIntroductionManage",
  components: {
    crudOperation,
    pagination,
    udOperation,
    Search,
    TrainingDialog
  },
  cruds() {
    return CRUD({
      title: "积分规则内容管理列表",
      url: "pointsRuleDescriptionRecord/getPointsRuleDescriptionRecordList",
      requestMethod: "post",
      sort: ["jobSort,asc2", "id,desc"],
      // takingType:'3',
      otherParams: { trainingType: "" },
      crudMethod: { ...crudJob }
    });
  },
  // mixins: [presenter()],
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ["TRAINING_TYPE"],
  data() {
    return {
      permission: {
        add: ["admin", "job:add"],
        edit: ["admin", "job:edit"],
        del: ["admin", "job:del"]
      }
    };
  },
  methods: {
    // 删除与修改事件
    handle(row, type) {
      if (type == "delete") {
        //删除
        this.deleteHandle(row);
      } else if (type == "update") {
        // 修改
        let forms = JSON.parse(JSON.stringify(row));
        setTimeout(() => {
          this.$refs.contentDialog.form = {
            id: forms.id,
            content: forms.content // 发布标志，0-未发布，1-发布
          };
          this.$refs.contentDialog.isEdit = true;
          this.$refs.contentDialog.dialogVisible = true;
          this.$refs.contentDialog.forms.content = forms.content;
        }, 100);
        console.log("🚀 ~ setTimeout ~  this.$refs:", this.$refs);
      } else if (type == "new") {
        // 新增
        this.$refs.contentDialog.form = {
          id: "",
          content: "" //描述
        };
        this.$refs.contentDialog.isEdit = false;
        this.$refs.contentDialog.dialogVisible = true;
      } else if (type == "batchDelete") {
        // 批量删除
        if (this.crud.selections.length == 0) {
          this.$notify({ title: "请选择需要删除的数据", type: "warning" });
          return;
        }
        const idList = [];
        for (let index = 0; index < this.crud.selections.length; index++) {
          const element = this.crud.selections[index];
          idList.push(element.id);
        }
        this.deleteHandle(idList);
      } else {
      }
    },
    // 删除
    deleteHandle(params) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        pointsRule
          .contentRuleDelete(params)
          .then(res => {
            this.$notify({ message: "操作成功", type: "success" });
            this.crud.refresh();
          })
          .catch(() => {});
      });
    },
    // 新建取模成功
    submitSuccess() {
      this.crud.refresh();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
.item {
  margin-top: 10px;
  margin-right: 10px;
}
</style>
